export default {
  inserted(el, binding) {
    const input = el.getElementsByTagName('input')[0]
    var regx = new RegExp('[^A-Za-z0-9-_' + (input.pattern || '') + ']', 'g')
    input.onkeyup = function(e) {
      input.value = input.value.replace(regx, '')
    }

    input.onblur = function(e) {
      input.value = input.value.replace(regx, '')
    }

    var isInputZh = false
    input.addEventListener(
      'compositionstart',
      function(e) {
        isInputZh = true
      },
      false
    )
    input.addEventListener(
      'compositionend',
      function(e) {
        isInputZh = false
        input.value = input.value.replace(regx, '')
      },
      false
    )
    input.addEventListener(
      'input',
      function(e) {
        if (isInputZh) return
        input.value = input.value.replace(regx, '')
      },
      false
    )
  }
}
